<template>
  <div>
    <el-form :inline="true" :model="searchMap" class="demo-form-inline">
        <el-form-item label="编号">
            <el-input v-model="searchMap.id" placeholder="请输入分类编号"></el-input>
        </el-form-item>
      <el-form-item label="规格名称">
        <el-input v-model="searchMap.name" placeholder="请输入规格名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <!-- <el-button type="primary" @click="goBack">返回</el-button> -->
        <el-button
          type="primary"
          @click="dialogFormVisible=true; specForm={parentId: searchMap.parentId}"
        >新增分类</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" width="75" prop="id"></el-table-column>
      <el-table-column label="名称" width="175" prop="name"></el-table-column>
      <el-table-column label="商品数量" width="100" prop="goodsNum"></el-table-column>
      <el-table-column label="是否显示" width="100">
         <template slot-scope="scope">
           <el-switch
            v-model="scope.row.isShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            disabled
          ></el-switch></template>
        </el-table-column>
      <el-table-column label="是否导航" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isMenu"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            disabled
          ></el-switch>
        </template>
        </el-table-column>
        <el-table-column label="级别" width="50">
            <template>
              {{parentIds.length+1}}
            </template>
        </el-table-column>
      <el-table-column label="品牌" width="75">
        <template slot-scope="scope">
          <span type="success" v-for="temp in tempList" :key="temp.id">
            <el-tag v-if="scope.row.templateId === temp.id">{{temp.name}}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="设置">
        <template slot-scope="scope">
          <el-button
            v-if="parentIds.length<2"
            size="mini"
            @click="queryByParentId(scope.row.id)"
          >查看下级</el-button>
          <el-button size="mini" type="danger" @click="addLL(scope.row.id)">新增下级</el-button>
          <el-button style="margin-top:5px;margin-left:0px;" size="mini" v-if="scope.row.parentId>0" type="primary" @click="returnQuery">返回上级</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizeList"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <el-dialog :title="specForm.id==null?'新增':'编辑'" :visible.sync="dialogFormVisible">
      <el-form :model="specForm">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="specForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" :label-width="formLabelWidth">
          <el-input v-model="specForm.goodsNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" :label-width="formLabelWidth">
          <el-switch
            v-model="specForm.isShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item label="是否导航" :label-width="formLabelWidth">
          <el-switch
            v-model="specForm.isMenu"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item label="seq" :label-width="formLabelWidth">
          <el-input v-model="specForm.seq" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属模板" :label-width="formLabelWidth">
          <template>
            <el-select id="slt" filterable name="templateId" style="width:150px" v-model="specForm.templateId">
              <el-option v-for="temp in tempList"
               :key="temp.id"
               :value="temp.id"
               :label="temp.name"></el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false; specForm={}">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      pageSizeList: [5, 10, 20, 30],
      total: 0,
      searchMap: { parentId: 0 },
      dialogFormVisible: false,
      specForm: {},
      xian: {},
      formLabelWidth: "150px",
      tempList: [],
      templateId: 0,
      parentIds: [],
      num: 0
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
      queryByParentId (parentId) {
        this.num -= 1
        this.parentIds.push(this.searchMap.parentId)
        this.searchMap.parentId = parentId
        this.getList()
      },
    async getTempList() {
      const { data: res } = await this.$http.get("goods/template/findAll");
      this.tempList = res.data;
    },
      returnQuery () {
        if (this.parentIds.length > 0) {
          this.searchMap.parentId = this.parentIds[this.parentIds.length - 1]
          this.parentIds.splice(this.parentIds.length - 1, 1)
          this.getList()
        }
      },
    async getList() {
      console.log(this.searchMap)
      this.num += 1
      const { data: res } = await this.$http.post(
        `goods/category/findPage/${this.currentPage}/${this.pageSize}`,
        this.searchMap
      );
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
    onSubmit() {
      this.currentPage = 1;
      this.getList();
    },
    async handleEdit(index, row) {
      this.dialogFormVisible = true;
      const id = row.id;
      const { data: res } = await this.$http.get(
        `goods/category/findById/${id}`
      );
      console.log(res)
      this.specForm = res;
    },
    addLL(parentId) {
      this.specForm = {}
      this.specForm.parentId = parentId
      this.dialogFormVisible = true;
    },
    async handleDelete(index, row) {
      const id = row.id;
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: pid } = await this.$http.get(
            `goods/category/selectParentId?parentId=${id}`
          );
          if (pid.data.length > 0) {
            this.$message({
              type: "warning",
              message: "存在下级，无法删除!"
            });
            return null;
          }
          const { data: res } = await this.$http.post(
            `goods/category/delete/${id}`
          );
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          } else {
            this.$message({
              type: "warning",
              message: res.message
            });
            this.getList();
          }
          })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    async handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    async save() {
      console.log(this.specForm)
      this.dialogFormVisible = false;
      const { data: res } = await this.$http.post(
        `goods/category/${this.specForm.id == null ? "insert" : "update"}`,
        this.specForm
      );
      if (res.code === 200) {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.getList();
      }
    },
    async dd() {
      const { data: res } = await this.$http.get(
        `goods/category/page?page=${this.currentPage}&size=${this.pageSize}`
      );
      this.xian = res.data.list;
      // 集合转数组
      // for (var i in this.xian) {
      //   this.tableData.push(this.xian[i])
      // }
      this.tableData = Object.values(this.xian);
      this.total = res.data.total;
    }
  },
  async created() {
    this.getTempList();
    this.getList();
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.item {
   margin: 4px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 250px;
  height: 178px;
  display: block;
}
</style>
